<?php
/* ***************************************
* curl�?用于像另一个程序发送数据，并继续原页面的运�?
* 运行所需参数如下
* $data[userid]="hello2221";   //参数1.$data[xxx]:数据数组
*    $data[name]="1121";  
$data[mobile]="19212311";  
$aim_url='http://127.0.0.1/b.php';  //2.接受数据并单独处理的页面
$curl_p = new Curl_Curl();  
$post = @$curl_p->post($aim_url, $data);//这里是b.php的访问地址，请自行修改  
$get = @$curl_p->get($aim_url);得到数据
*************************************************/
class Curl_Curl {
	function Curl_Curl() {
		return true;
	}
	function execute($method, $url, $fields = '', $userAgent = '', $httpHeaders = '', $username = '', $password = '') {
		$ch = Curl_Curl::create();
		if (false === $ch) {
			return false;
		}
		if (is_string($url) && strlen($url)) {
			$ret = curl_setopt($ch, CURLOPT_URL, $url);
		} else {
			return false;
		} //是否显示头部信息  
		curl_setopt($ch, CURLOPT_HEADER, false); //  
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		if ($username != '') {
			curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
		}
		$method = strtolower($method);
		if ('post' == $method) {
			curl_setopt($ch, CURLOPT_POST, true);
			if (is_array($fields)) {
				$sets = array();
				foreach($fields AS $key =>$val){
					$sets[] = $key.'='.urlencode($val);
				}
				$fields = implode('&', $sets);
			}
			curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
		} else if ('put' == $method) {
			curl_setopt($ch, CURLOPT_PUT, true);
		} //curl_setopt($ch, CURLOPT_PROGRESS, true);  
		//curl_setopt($ch, CURLOPT_VERBOSE, true);  
		//curl_setopt($ch, CURLOPT_MUTE, false);  
		curl_setopt($ch, CURLOPT_TIMEOUT, 20); //设置curl超时秒数，例如将信息POST出去3秒钟后自动结束运行�? 
		if (strlen($userAgent)) {
			curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
		}
		if (is_array($httpHeaders)) {
			curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
		}
		$ret = curl_exec($ch);
		if (curl_errno($ch)) {
			$ret = array(0=>curl_error($ch),1=>curl_errno($ch));
			curl_close($ch);
			return $ret;
		} else {
			curl_close($ch);
			if (!is_string($ret) || !strlen($ret)) {
				return false;
			}
			return $ret;
		}
	}
	function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '', $password = '') {
		$ret = Curl_Curl::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username, $password);
		if (false === $ret) {
			return false;
		}
		if (is_array($ret)) {
			return false;
		}
		return $ret;
	}
	function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = '') {
		$ret = Curl_Curl::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password);
		if (false === $ret) {
			return false;
		}
		if (is_array($ret)) {
			return false;
		}
		return $ret;
	}
	function create() {
		$ch = null;
		if (!function_exists('curl_init')) {
			return false;
		}
		$ch = curl_init();
		if (!is_resource($ch)) {
			return false;
		}
		return $ch;
	}
}
/*
class CURL {
	var $callback = false;
	function setCallback($func_name) {
		$this - >callback = $func_name;
	}
	function doRequest($method, $url, $vars) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie_w.txt');
		curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie_w.txt'); //curl_setopt($ch, CURLOPT_WRITEHEADER, 'E:\\web\\cookie.txt');
		if ($method == 'POST') {
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
		}
		$data = curl_exec($ch);
		curl_close($ch);
		if ($data) {
			if ($this - >callback) {
				$callback = $this - >callback;
				$this - >callback = false;
				return call_user_func($callback, $data);
			} else {
				return $data;
			}
		} else {
			return curl_error($ch);
		}
	}
	function get($url) {
		return $this - >doRequest('GET', $url, 'NULL');
	}
	function post($url, $vars) {
		return $this - >doRequest('POST', $url, $vars);
	}
} 
*/

?>
